﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApDisAutomaticDisassemblyApp
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Collections;
using System.Collections.ObjectModel;
using System.IO;
using System.Resources;
using System.Windows.Forms;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

public class CApDisAutomaticDisassemblyApp
{
  private static bool m_isUserAskedThisSession;
  private ITxOperation m_targetOperation;
  private const string RESOURCE_TABLE_PATH = "DnProcessSimulateCommands.AutomaticDisassembly.ResourceTable";
  private const string ROOT_ACTIVITY_TYPE = "MEActivity";

  public CApDisAutomaticDisassemblyApp(ITxOperation targetOperation)
  {
    this.m_targetOperation = targetOperation;
  }

  static CApDisAutomaticDisassemblyApp()
  {
    TxApplication.DocumentCollection.DocumentLoaded += new TxDocumentCollection_DocumentLoadedEventHandler(CApDisAutomaticDisassemblyApp.DocumentCollection_DocumentLoaded);
  }

  public string GetDisassemblyRootName()
  {
    return TxAutomaticDisassembly.DynamicRoot != null ? TxAutomaticDisassembly.DynamicRoot.Name : string.Empty;
  }

  public bool AreGlobalDirectionDefined()
  {
    return TxAutomaticDisassembly.Directions != null && TxAutomaticDisassembly.Directions.Length != 0;
  }

  public void GenerateShortestPathSequence(
    ITxObject partToBeReached,
    CApDisAutomaticDisassemblyDlgData configurationData)
  {
    TxAutomaticDisassemblySequenceCreationData sequenceData = this.DialogDataToSequenceData(configurationData);
    TxAutomaticDisassembly.CreateShortestPathSequenceGeneration((ITxObject) this.m_targetOperation, partToBeReached, sequenceData);
  }

  public bool GenerateShortestPathSequence(
    DialogResult dialogResult,
    ITxObject partToBeReached,
    CApDisAutomaticDisassemblyDlgData configurationData)
  {
    bool shortestPathSequence;
    switch (dialogResult)
    {
      case DialogResult.Cancel:
        shortestPathSequence = false;
        break;
      case DialogResult.Yes:
        TxAutomaticDisassembly.RecalculateDirectionalBlockingConstraintsForEntireTree(partToBeReached);
        this.GenerateShortestPathSequence(partToBeReached, configurationData);
        shortestPathSequence = true;
        CApDisAutomaticDisassemblyApp.m_isUserAskedThisSession = true;
        break;
      case DialogResult.No:
        TxAutomaticDisassembly.UpdateDirectionalBlockingConstraintsForEntireTree(partToBeReached);
        this.GenerateShortestPathSequence(partToBeReached, configurationData);
        shortestPathSequence = true;
        CApDisAutomaticDisassemblyApp.m_isUserAskedThisSession = true;
        break;
      default:
        shortestPathSequence = false;
        break;
    }
    return shortestPathSequence;
  }

  public DialogResult Generate(
    CApDisAutomaticDisassemblyDlgData configurationData,
    ITxObject partToBeReached,
    CUiDisAutomaticDisassemblyForm.SequenceGenerationMode mode)
  {
    bool flag = false;
    TxObjectList unmovableParts = new TxObjectList();
    DialogResult dialogResult = this.ShowWarningDialog();
    if (dialogResult != DialogResult.Cancel)
    {
      TxApplication.RefreshDisplay();
      Application.DoEvents();
      Cursor current = Cursor.Current;
      Cursor.Current = Cursors.WaitCursor;
      TxApplicationEx.FireDocumentUpdateStarted();
      try
      {
        flag = mode != CUiDisAutomaticDisassemblyForm.SequenceGenerationMode.Disassembly ? this.GenerateShortestPathSequence(dialogResult, partToBeReached, configurationData) : this.GenerateSequence(dialogResult, configurationData, out unmovableParts);
      }
      catch
      {
      }
      finally
      {
        TxApplicationEx.FireDocumentUpdateEnded();
        Cursor.Current = current;
        if (((Collection<ITxObject>) unmovableParts).Count == 0)
        {
          if (flag)
            this.ShowSuccessMessage();
          else
            this.ShowFailureMessage();
        }
        else
          this.ShowDisassemblyImpossibleMessage();
      }
    }
    return dialogResult;
  }

  public object[] GetAllowedActivityTypes()
  {
    object[] allowedActivityTypes = (object[]) null;
    TxPlanningTypeMetaData typeMetaData = TxApplication.ActiveDocument.PlatformGlobalServicesProvider.GetTypeMetaData("MEActivity");
    if (typeMetaData != null)
    {
      int count1 = typeMetaData.AllowedChildrenTypes.Count;
      SortedList sortedList = new SortedList();
      for (int index = 0; index < count1; ++index)
      {
        if (typeMetaData.AllowedChildrenTypes[index] is TxPlanningTypeMetaData allowedChildrenType)
          sortedList.Add((object) allowedChildrenType.DisplayName, (object) allowedChildrenType.TypeName);
      }
      int count2 = sortedList.Count;
      allowedActivityTypes = new object[count2];
      for (int index = 0; index < count2; ++index)
        allowedActivityTypes[index] = sortedList.GetKey(index);
    }
    return allowedActivityTypes;
  }

  private DialogResult ShowWarningDialog() => this.ShowAutomaticDisassemblyWarrningMessage();

  private DialogResult ShowAutomaticDisassemblyWarrningMessage()
  {
    ResourceManager resourceManager = this.GetResourceManager();
    return TxMessageBox.Show($"{resourceManager.GetString("AUTOMATIC_DISSASEMBLY_MESSAGEBOX_QUESTION") + "\n\n"}{resourceManager.GetString("AUTOMATIC_DISSASEMBLY_MESSAGEBOX_NOTE")}\n\n" + resourceManager.GetString("AUTOMATIC_DISSASEMBLY_MESSAGEBOX_EXPLANATION"), resourceManager.GetString("AUTOMATIC_DISASSEMBLY_MESSAGEBOX_CAPTION"), MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
  }

  private bool GenerateSequence(
    DialogResult dialogResult,
    CApDisAutomaticDisassemblyDlgData configurationData,
    out TxObjectList unmovableParts)
  {
    unmovableParts = new TxObjectList();
    bool sequence;
    switch (dialogResult)
    {
      case DialogResult.Cancel:
        sequence = false;
        break;
      case DialogResult.Yes:
        TxAutomaticDisassembly.RecalculateDirectionalBlockingConstraints();
        this.GenerateSequence(configurationData, out unmovableParts);
        sequence = true;
        CApDisAutomaticDisassemblyApp.m_isUserAskedThisSession = true;
        break;
      case DialogResult.No:
        TxAutomaticDisassembly.UpdateDirectionalBlockingConstraints();
        this.GenerateSequence(configurationData, out unmovableParts);
        sequence = true;
        CApDisAutomaticDisassemblyApp.m_isUserAskedThisSession = true;
        break;
      default:
        sequence = false;
        break;
    }
    return sequence;
  }

  private void GenerateSequence(
    CApDisAutomaticDisassemblyDlgData configurationData,
    out TxObjectList unmovableParts)
  {
    TxAutomaticDisassembly.CreateSequence((ITxObject) this.m_targetOperation, this.DialogDataToSequenceData(configurationData), ref unmovableParts);
  }

  private void ShowSuccessMessage()
  {
    int num = (int) TxMessageBox.Show(this.GetResourceManager().GetString("AUTOMATIC_DISASSEMBLY_GENERATION_FINISHED_SUCCSSESS"), this.GetResourceManager().GetString("AUTOMATIC_DISSASEMBLY_INFO"), MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  }

  private void ShowFailureMessage()
  {
    int num = (int) TxMessageBox.Show(this.GetResourceManager().GetString("AUTOMATIC_DISASSEMBLY_GENERATION_FINISHED_FAILD"), this.GetResourceManager().GetString("AUTOMATIC_DISSASEMBLY_INFO"), MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  }

  private void ShowDisassemblyImpossibleMessage()
  {
    string str1 = this.GetResourceManager().GetString("AUTOMATIC_DISASSEMBLY_GENERATION_IMPOSSIBLE");
    string str2 = "AutomaticDisassembly.txt";
    string str3 = Path.GetTempPath() + str2;
    string str4 = this.GetResourceManager().GetString("AUTOMATIC_DISSASEMBLY_INFO");
    int num = (int) TxMessageBox.Show(str1 + str3, str4, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  }

  private TxAutomaticDisassemblySequenceCreationData DialogDataToSequenceData(
    CApDisAutomaticDisassemblyDlgData dialogData)
  {
    return new TxAutomaticDisassemblySequenceCreationData()
    {
      DistanceMode = (TxAutomaticDisassemblySequenceCreationData.TxAutomaticDisassemblyDistanceMode) dialogData.DistanceMode,
      SequenceType = (TxAutomaticDisassemblySequenceCreationData.TxAutomaticDisassemblySequenceType) dialogData.DirectionType,
      DistanceValue = dialogData.DistanceValue,
      OperationDuration = dialogData.OperationDuration,
      OperationType = dialogData.OperationType,
      UseAutomaticPathPlanner = dialogData.UseAutomaticPathPlanner,
      Precision = dialogData.Precision,
      Clearance = dialogData.Clearance
    };
  }

  private ResourceManager GetResourceManager()
  {
    return new ResourceManager("DnProcessSimulateCommands.AutomaticDisassembly.ResourceTable", this.GetType().Assembly);
  }

  private static void DocumentCollection_DocumentLoaded(
    object sender,
    TxDocumentCollection_DocumentLoadedEventArgs args)
  {
    CApDisAutomaticDisassemblyApp.m_isUserAskedThisSession = false;
  }
}
